package at.fhooe.mc.sn.gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import at.fhooe.mc.sn.ProtocolHandler;

public class Acc extends JPanel implements ActionListener {

	private JLabel x_label;
	private JLabel maxX;
	private JLabel y_label;
	private JLabel maxY;
	private JLabel z_label;
	private JLabel maxZ;
	private JLabel maxTotal;
	
	private ProtocolHandler protocol;

	/**
	 * Create the panel.
	 * @param protocol 
	 */
	public Acc(ProtocolHandler protocol) {
		this.protocol = protocol;
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(23, 55, 353, 164);
		add(panel);
		panel.setLayout(new GridLayout(4, 3, 0, 0));
		
		JLabel lblValues = new JLabel("");
		panel.add(lblValues);
		lblValues.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		JLabel lbl = new JLabel("");
		panel.add(lbl);
		lbl.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		JLabel lblMax = new JLabel("Max");
		lblMax.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(lblMax);
		
		JLabel lblY = new JLabel("x");
		panel.add(lblY);
		lblY.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		x_label = new JLabel("");
		panel.add(x_label);
		x_label.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		maxX = new JLabel("");
		maxX.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(maxX);
		
		JLabel lblX = new JLabel("y");
		panel.add(lblX);
		lblX.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		y_label = new JLabel("");
		panel.add(y_label);
		y_label.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		maxY = new JLabel("");
		maxY.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(maxY);
		
		JLabel lblZ = new JLabel("z");
		panel.add(lblZ);
		lblZ.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		z_label = new JLabel("");
		panel.add(z_label);
		z_label.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		maxZ = new JLabel("");
		maxZ.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(maxZ);
		
		JLabel lblAccelerometer = new JLabel("Accelerometer");
		lblAccelerometer.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblAccelerometer.setBounds(140, 11, 159, 29);
		add(lblAccelerometer);
		
		JLabel lblNewLabel = new JLabel("Max Total Acc.");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel.setBounds(118, 224, 124, 29);
		add(lblNewLabel);
		
		maxTotal = new JLabel("");
		maxTotal.setFont(new Font("Tahoma", Font.PLAIN, 17));
		maxTotal.setBounds(259, 224, 117, 29);
		add(maxTotal);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.setBounds(351, 11, 89, 23);
		add(btnRefresh);
		btnRefresh.addActionListener(this);
		
		refresh();
		
		setVisible(true);
	}
	
	public void refresh(){
		x_label.setText(String.valueOf(protocol.requestDouble("REQX")));
		maxX.setText(String.valueOf(protocol.requestDouble("REQPARA:30")));
		y_label.setText(String.valueOf(protocol.requestDouble("REQY")));
		maxY.setText(String.valueOf(protocol.requestDouble("REQPARA:31")));
		z_label.setText(String.valueOf(protocol.requestDouble("REQZ")));
//		maxZ.setText(String.valueOf(protocol.requestDouble("REQPARA:32")));
		maxTotal.setText(String.valueOf(protocol.requestDouble("REQPARA:11")));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		refresh();
	}
}
